package com.lsz.flight_system.controller;

import com.lsz.flight_system.entity.Notice;
import com.lsz.flight_system.entity.PlaneMessage;
import com.lsz.flight_system.entity.UserMessage;
import com.lsz.flight_system.service.NoticeService;
import com.lsz.flight_system.service.PlaneMessageService;
import com.lsz.flight_system.service.UserMessageService;
import com.lsz.flight_system.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserMessageService userMessageService;

    @Autowired
    PlaneMessageService planeMessageService;

    @GetMapping("/userNotices")
    @ResponseBody
    public Result userNotices(HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");
//        Map<String,Object> queryMap = new HashMap<>();
//        queryMap.put("belong",userMessage.getUsername());
        if (userMessage==null){return Result.fail("请登录后进行查看");}

        List<Notice> noticeList = noticeService.findNoticesByBelong(userMessage.getUsername());

        return Result.ok(noticeList);
    }

    @GetMapping("/adminNotices")
    @ResponseBody
    public Result adminNotices(@RequestParam(value = "sender",required = false) String sender){
        Map<String,Object> queryMap = new HashMap<>();

        queryMap.put("sender",sender);
        queryMap.put("belong","管理员");

        List<Notice> noticeList = noticeService.findAllByNotice(queryMap);

        return Result.ok(noticeList);
    }

    @ResponseBody
    @GetMapping("/userSendNotice")
    public Result userSendNotice(@RequestParam(value = "noticeText") String noticeText,
                                 HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");

        if (userMessage==null){
            return Result.fail("请登录后进行评论");
        }
        String text = noticeText.replace(" ","").replace("\n","").replace("\r","");
        if (text.equals("")){
            return Result.fail("发送的值不能为空");
        }

        Notice notice = new Notice();

        notice.setNoticeText(noticeText);
        notice.setBelong("管理员");
        notice.setSender(userMessage.getUsername());

        if (noticeService.saveNotice(notice)){
            return Result.ok();
        }
        return Result.fail("用户发送通知异常");
    }

    @ResponseBody
    @GetMapping("/adminSendNotice/byUsername")
    public Result adminSendNoticeByUsername(@RequestParam(value = "username") String username,
                                            @RequestParam(value = "noticeText") String noticeText){
        Notice notice = new Notice();
        String text = noticeText.replace(" ","").replace("\n","").replace("\r","");
        UserMessage userMessage = userMessageService.getUserMessageByUsername(username);

        if (userMessage==null){
            return Result.fail("要发送的用户名不存在，请输入正确的用户名");
        }
        //在判断用户名之前判断值，如果值有问题可省去一次数据库查询，这里暂未移动
        if (text.equals("")){
            return Result.fail("发送的值不能为空");
        }
        notice.setBelong(username);
        notice.setSender("管理员");
        notice.setNoticeText(noticeText);

        if (noticeService.saveNotice(notice)){
            return Result.ok();
        }
        return Result.fail("管理员发送通知异常");
    }

    @ResponseBody
    @GetMapping("/adminSendNotice/byPlaneId")
    public Result adminSendNoticeByPlaneId(@RequestParam(value = "planeId") String planeId,
                                           @RequestParam(value = "noticeText") String noticeText){
        PlaneMessage planeMessage = planeMessageService.findByPlaneId(planeId);
        String text = noticeText.replace(" ","").replace("\n","").replace("\r","");

        if (planeMessage==null){
            return Result.fail("航班号对应航班不存在，请输入正确的航班号");
        }
        if (text.equals("")){
            return Result.fail("发送的值不能为空");
        }
        noticeService.adminSaveNoticeByPlaneId(planeId,noticeText);
        return Result.ok();
    }

    @PostMapping("/adminDelNotice/{id}")
    public Result adminDelNoticeById(@PathVariable Long id){
        noticeService.adminDelNoticeById(id);
        return Result.ok();
    }

    @PostMapping("/delNotice/{id}")
    public Result delNoticeById(@PathVariable Long id){
        if (noticeService.delNoticeById(id)){
            return Result.ok();
        }
        return Result.fail("操作异常");
    }
}
